////////////////////////////////////////////////////////////////////////////////
// Copyright 2010 Michael Schmalle - Teoti Graphix, LLC
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0 
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and 
// limitations under the License
// 
// Author: Michael Schmalle, Principal Architect
// mschmalle at teotigraphix dot com
////////////////////////////////////////////////////////////////////////////////
package fxplugins.registry.util
{
import fxplugins.plugin.PluginOptions;
import fxplugins.registry.IExtension;
import fxplugins.registry.IExtensionDescriptor;
import fxplugins.registry.IExtensionPoint;
import fxplugins.registry.IExtensionRegistry;

/**
 * @private
 */
public class ExtensionRegistryUtil
{
	public function ExtensionRegistryUtil()
	{
	}
	
	public static function toOptionsArray(list:Array):Array
	{
		var result:Array = [];
		
		for (var i:int = 0; i < list.length; i++)
		{
			var element:IExtensionDescriptor = list[i] as IExtensionDescriptor;
			if (element.options is PluginOptions)
			{
				PluginOptions(element.options).descriptor = element;
			}
			result.push(element.options);
		}
		
		return result;
	}
	
	public static function getDescriptorBy(registry:IExtensionRegistry,
										   extensionPointID:String, 
										   descriptorID:String):IExtensionDescriptor
	{
		var point:IExtensionPoint = registry.getExtensionPoint(extensionPointID);
		if (point == null)
			return null;
		
		var descriptors:Array = point.getExtensionDescriptors();
		for each (var descriptor:IExtensionDescriptor in descriptors)
		{
			if (descriptor.uid == descriptorID)
				return descriptor;
		}
		
		return null;
	}
	
	public static function getDescriptorByID(registry:IExtensionRegistry,
											 extensionPointID:String, 
											 extensionID:String, 
											 descriptorID:String):IExtensionDescriptor
	{
		var point:IExtensionPoint = registry.getExtensionPoint(extensionPointID);
		if (point == null)
			return null;
		
		var extension:IExtension = point.getExtension(extensionID);
		if (extension == null)
			return null;
		
		var descriptors:Array = extension.getExtensionDescriptors();
		if (descriptors == null)
			return null;
		
		for each (var descriptor:IExtensionDescriptor in descriptors)
		{
			if (descriptor.uid == descriptorID)
				return descriptor;
		}
		
		return null;
	}
}
}